﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

namespace MapCenterSystem.DataInfo
{
    public class MapInfo
    {
        Point earthPoint;
        Point tile;
        Size size;
        string mapStore;
        int zoom;
        string source;
        string sourceBackup;

        public string SourceBackup
        {
            get { return sourceBackup; }
            set { sourceBackup = value; }
        }


        public string Source
        {
            get { return source; }
            set { source = value; }
        }

        public Point TileStartPoint
        {
            get
            {
                Point titeStartPoint= new Point(earthPoint.X - earthPoint.X % 256, earthPoint.Y - earthPoint.Y % 256);
                return titeStartPoint;
            }
        }

        public Point EarthPoint
        {
            get { return earthPoint; }
            set { earthPoint = value; }
        }

        public Point Tile
        {
            get
            {
                return tile;
            }
            set
            {
                tile = value;
            }
        }

        public Point GetTileFromEarthPoint()
        {
            tile = new Point(earthPoint.X / 256, earthPoint.Y / 256);
            return tile; 
        }
        
        public Size Size
        {
            get { return size; }
            set { size = value; }
        }
        
        public string MapStore
        {
            get { return mapStore; }
            set 
            { 
                mapStore = value;
                if (!Directory.Exists(mapStore))
                    Directory.CreateDirectory(mapStore);
            }
        }

        public int Zoom
        {
            get 
            { 
                return zoom; 
            }
            set 
            { 
                zoom = value; }
        }

        public string FullPath
        {
            get 
            {
                string name=string.Format("{0}_{1}_{2}.map", zoom, Tile.X, Tile.Y);
                string path = Path.Combine(mapStore, name);
                return path;
            }
        }
        
        
        public Image GetMapFromByte(byte[] data)
        {
            MemoryStream memStream = new MemoryStream(data);
            Bitmap map = new Bitmap(memStream);
            return map;
        }
        
        public void SaveImage(Image image)
        {
            if (File.Exists(FullPath))
                return;
            Bitmap bitmap = new Bitmap(image);
            bitmap.Save(FullPath, ImageFormat.Png);
            bitmap.Dispose();
        }

        public byte[] ConvertToByte(Image image)
        {
            Bitmap map = new Bitmap(image);
            MemoryStream ms = new MemoryStream();
            map.Save(ms, ImageFormat.Jpeg);
            byte[] byteResult = ms.ToArray();
            ms.Close();
            map.Dispose();
            return byteResult;
        }

        public byte[] ConvertToByte()
        {
            Bitmap map = new Bitmap(FullPath);
            MemoryStream ms = new MemoryStream();
            map.Save(ms, ImageFormat.Png);
            byte[] byteResult = ms.ToArray();
            ms.Close();
            map.Dispose();
            return byteResult;

        }

        public bool Exist()
        {
            return File.Exists(FullPath);
        }

    }
}
